package xiangmu.schoolpaper.service;

import org.springframework.http.MediaType;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.reactive.function.BodyInserters;
import org.springframework.web.reactive.function.client.WebClient;

import java.io.IOException;

@Service
public class UploadFilesService {
    private final WebClient webClient;

    public UploadFilesService(WebClient webClient) {
        this.webClient = webClient;
    }
    
    public String uploadPapersToFlask(MultipartFile file, String upload_id) throws IOException {
        String flaskUrl = "http://localhost:5001/uploadPaper";

        return webClient.post()
                .uri(flaskUrl)
                .contentType(MediaType.MULTIPART_FORM_DATA)
                .body(BodyInserters.fromMultipartData("file", file.getResource())
                    .with("upload_id", upload_id)
                )
                .retrieve()
                .bodyToMono(String.class)
                .block(); // 阻塞，返回字符串
    }
}



